package com.all.play.myapplication.moudle.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.all.play.myapplication.moudle.bean.UserInfo;
import com.all.play.myapplication.moudle.db.UserAccountDB;

/**
 * Created by 聪明一只哈 on 2018/3/30.
 * 帐号数据库的操作类
 */

public class UserAccountDao {

    private UserAccountDB mHelper;

    public UserAccountDao(Context context) {
        mHelper = new UserAccountDB(context);
    }

    // 添加用户到数据库
    public void addAccount(UserInfo user){
        SQLiteDatabase database = mHelper.getReadableDatabase();
        ContentValues values=new ContentValues();
        values.put(UserAccountTable.COL_HXID,user.getHxid());
        values.put(UserAccountTable.COL_NAME,user.getName());
        values.put(UserAccountTable.COL_NICK,user.getNick());
        values.put(UserAccountTable.COL_PHOTO,user.getPhoto());
        database.replace(UserAccountTable.TAB_NAME,null,values);

    }

    // 根据环信id获取所有用户信息
    public UserInfo getAccountByHxId(String hxId){
        SQLiteDatabase database = mHelper.getReadableDatabase();

        //查询
        Cursor cursor = database.query(UserAccountTable.TAB_NAME,
                null, UserAccountTable.COL_HXID + "=?"
                , new String[]{hxId}, null, null,null);
        /*String sql="select * from "+UserAccountTable.TAB_NAME+" where "+ UserAccountTable.COL_HXID +" =?";

        Cursor cursor =database.rawQuery(sql,new String[]{hxId});*/
        UserInfo userInfo = new UserInfo();
        if(cursor!=null){
            while (cursor.moveToNext()){

                userInfo.setHxid(cursor.getString(cursor.getColumnIndex(UserAccountTable.COL_HXID)));
                userInfo.setName(cursor.getString(cursor.getColumnIndex(UserAccountTable.COL_NAME)));
                userInfo.setNick(cursor.getString(cursor.getColumnIndex(UserAccountTable.COL_NICK)));
                userInfo.setPhoto(cursor.getString(cursor.getColumnIndex(UserAccountTable.COL_PHOTO)));

            }
        }

        //关闭资源
        cursor.close();
        //返回数据
        return userInfo;
    }


}
